package me.whf.serivce;

import java.util.Collections;
import java.util.List;

import me.whf.dao.AccountDAO;
import me.whf.dao.LoggerDAO;
import me.whf.dao.factory.DAOFactory;
import me.whf.model.Account;
import me.whf.model.Logger;
import me.whf.util.Utils;
import me.whf.util.Utils.AccountType;
import me.whf.util.Utils.UserType;

public class AccountService {

	public static String createAccount(String username, String identity,
			UserType userType, AccountType accountType, double balance,
			String password, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		String accountID;
		do {
			accountID = Utils.generateRandomAccount();
		} while (accountDAO.isExisted(accountID));
		
		Account account = new Account(username, identity, userType, balance, accountID, password, accountType);
		if (accountDAO.createAccount(account)) {
			loggerDAO.log(employeeID, "创建用户" + accountID, accountID);
		
			return "创建用户：" + accountID;
		} else {
			return "创建用户失败";
		}
	}

	public static String deposit(String accountID, String password, double money, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		if (accountDAO.deposit(accountID, password, money)) {
			double balance = accountDAO.query(accountID, password);
			loggerDAO.log(employeeID, accountID + "存款：" + money + "，账户余额：" + balance, accountID);
			
			return "存款：" + money + "，账户余额：" + balance;
		} else {
			return "存款失败";
		}
	}

	public static String withdraw(String accountID, String password, double money, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		if (accountDAO.canWithdraw(accountID, password, money)) {
			if (accountDAO.withdraw(accountID, password, money)) {
				double balance = accountDAO.query(accountID, password);
				loggerDAO.log(employeeID, accountID + "取款：" + money + "，账户余额：" + balance, accountID);
				
				return "取款：" + money + "，账户余额：" + balance;
			}
		} 
		return "取款失败";
	}

	public static String query(String identity, String accountID, String password, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		double balance = accountDAO.query(identity, accountID, password);
		if (balance != Integer.MIN_VALUE) {
			loggerDAO.log(employeeID, accountID + "查询账户，账户余额为" + balance, accountID);
			
			return "账户余额为" + balance;
		}
		return "查询失败";
	}

	public static String queryTimeline(String identity, String accountID,
			String password, String startDate, String endDate, String employeeID) {
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		List<Logger> logs = loggerDAO.query(identity, accountID, password, startDate, endDate);
		
		Collections.sort(logs);
		StringBuilder sb = new StringBuilder();
		for (Logger logger: logs) {
			sb.append(logger.getLogDate() + " " + logger.getContent() + '\n');
		}
		loggerDAO.log(employeeID, accountID + "查询流水", accountID);
		return sb.toString();
	}

	public static String transfer(String accountID, String identity,
			String password, String username, String receiverAccountID,
			String receiverUsername, double money, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		if (accountDAO.canWithdraw(accountID, password, money)) {
			if (accountDAO.transfer(accountID, identity, password, username, 
					receiverAccountID, receiverUsername, money)) {
				double balance = accountDAO.query(accountID, password);
				loggerDAO.log(employeeID, accountID + "转账：" + money + "到：" + receiverAccountID + "，账户余额：" + balance, accountID);
				
				return "转账：" + money + "到：" + receiverAccountID + "，账户余额：" + balance;
			}
		}
		return "转账失败";
	}

	public static String changePassword(String identity, String accountID, String oldPassword, String password, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		if (accountDAO.changePassword(identity, accountID, oldPassword, password)) {
			loggerDAO.log(employeeID, accountID + "更改密码成功", accountID);
			
			return "更改密码成功";
		} else {
			return "更改密码失败";
		}
	}
	
	public static String addEnterpriseOperator(String accountID, String enterpriseIdentity, String ownerPassword, String operatorPassword, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		if (accountDAO.addEnterpriseOperator(accountID, enterpriseIdentity, ownerPassword, operatorPassword)) {
			loggerDAO.log(employeeID, "企业" + enterpriseIdentity + "添加了新操作人", accountID);
			
			return "添加操作人成功";
		} else {
			return "添加操作人失败";
		}
	}

	public static String destory(String identity, String accountID, String password, String employeeID) {
		AccountDAO accountDAO = DAOFactory.getDAOFactory().getAccountDAO();
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		
		double balance = accountDAO.query(accountID, password);
		if (accountDAO.destory(identity, accountID, password)) {
			loggerDAO.log(employeeID, accountID + "销户成功，已取出金额" + balance, accountID);
			
			return "销户成功，已取出金额" + balance;
		} else {
			return "销户失败";
		}
	}

}
